## BTW

rm(list = ls())

library(tidyverse)
library(fixest)

## Get saved results
## Note that these results are produce by a separate script

res <- read_rds("data/Results_Main.rds")

## Prep for plot

res_plot <- res %>%
    filter(str_detect(outcome, "polar_bund_diff|small_party|turnout")) %>%
    filter(!str_detect(outcome, "small_party2")) %>%
    filter(period_numeric == 1) %>%
    filter(str_detect(term, "Decrease")) %>%
    mutate(outcome = dplyr::recode(outcome,
        `small_party_diff` = "Small party vote share",
        `polar_bund_diff` = "Polarization",
        `turnout_diff` = "Turnout"
    )) %>%
    filter(main_spec == 1) %>%
    mutate(ss = str_replace(ss, "05", "00")) %>%
    filter(!str_detect(unit, "State"))


## PD

pd <- position_dodge(0.4)

## Figure 2

p1 <- ggplot(
    res_plot %>%
        filter(ss == "Combined") %>%
        filter(!outcome == "Turnout"),
    aes(unit, estimate)
) +
    geom_hline(yintercept = 0, linetype = "dotted") +
    geom_errorbar(aes(ymin = conf.low90, ymax = conf.high90),
        position = pd, width = 0, linewidth = 1
    ) +
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
        position = pd, width = 0, linewidth = 0.5
    ) +
    geom_point(
        position = pd,
        shape = 21,
        fill = "white",
        size = 3
    ) +
    facet_wrap(~outcome, scales = "free_y", ncol = 2) +
    theme(legend.position = "bottom") +
    theme_bw() +
    xlab("") +
    ylab("Effect of newspaper exit") +
    theme(legend.position = "bottom")
p1

## Figure 3

p2 <- ggplot(
    res_plot %>%
        filter(str_detect(ss, "00")) %>%
        filter(!outcome == "Turnout") %>%
        filter(str_detect(unit, "Federal")) %>%
        mutate(ss = factor(ss, levels = unique(ss))),
    aes(ss, estimate)
) +
    geom_hline(
        yintercept = 0,
        linetype = "dotted"
    ) +
    geom_errorbar(aes(ymin = conf.low90, ymax = conf.high90),
        width = 0,
        linewidth = 1
    ) +
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
        width = 0,
        linewidth = 0.5
    ) +
    geom_point(
        shape = 21,
        size = 3,
        fill = "white"
    ) +
    facet_wrap(~outcome, scales = "free_y", ncol = 2) +
    theme(legend.position = "bottom") +
    theme_bw() +
    xlab("") +
    ylab("Effect of newspaper exit") +
    theme(legend.position = "bottom")
p2

## Figure A12

p3 <- ggplot(
    res_plot %>%
        filter(ss == "Combined") %>%
        filter(outcome == "Turnout"),
    aes(unit, estimate)
) +
    geom_hline(yintercept = 0, linetype = "dotted") +
    geom_errorbar(aes(ymin = conf.low90, ymax = conf.high90),
        position = pd, width = 0, linewidth = 1
    ) +
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
        position = pd, width = 0, linewidth = 0.5
    ) +
    geom_point(
        position = pd,
        shape = 21,
        fill = "white",
        size = 3
    ) +
    theme(legend.position = "bottom") +
    theme_bw() +
    xlab("") +
    ylab("Effect of\nnewspaper exit") +
    theme(legend.position = "bottom")
p3


